<?php

/**
 * Class which handles the remote validation and identifier process
 */
class RemoteHelper {
	
	/**
	 * This method checks if an instance is reachable through the com_easiv2 extension of the managed instance
	 * 
	 * @param $http_address frontend base URL of the Joomla! instance 
	 * @return true or false
	 */
	function isInstanceResponding($http_address) {
		$requestMethod = "checkCommunication.isInstanceResponding";
		
		$opts = array('http' =>	array(
		        'method'  => 'POST',
		        'header'  => 'Content-type: application/x-www-form-urlencoded',
			)
		);

		$context  = stream_context_create($opts);	
		@$response = file_get_contents($http_address."index.php?option=com_easiv2&task=".$requestMethod."&format=xmlrpc", false, $context);
		
		return $response;
	}
	
	/**
	 * Set the identifier on the remote instance
	 * 
	 * @param HTTP address
	 * @param current identifier
	 * @param new identifier
	 * @return response from remote instance
	 */
	function setIdentifier($http_address, $current_identifier, $new_identifier) {
		$requestMethod = "checkCommunication.setIdentifier";
		$request_param = xmlrpc_encode(array($current_identifier, $new_identifier));
		
		$postdata = http_build_query(array(
				'ids' => $request_param,
			)
		);

		$opts = array('http' =>	array(
		        'method'  => 'POST',
		        'header'  => 'Content-type: application/x-www-form-urlencoded',
				'content' => $postdata
			)
		);

		$context  = stream_context_create($opts);
		@$response = file_get_contents($http_address."index.php?option=com_easiv2&task=".$requestMethod."&format=xmlrpc", false, $context);
		
		return $response;
	}
}
?>